package com.xzs.service.impl;

import com.xzs.annaotation.XzsAutowired;
import com.xzs.annaotation.XzsService;
import com.xzs.service.LYTService;
import com.xzs.service.TimeService;

import java.util.concurrent.*;

/**
 * Created by zs.xu on 2020/8/13.
 */
@XzsService("LYTService")
public class LYTServiceImpl implements LYTService {
    @XzsAutowired("TimeService")
    private TimeService timeService;
    private ExecutorService executorService = new ThreadPoolExecutor(10, 10, 0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(1024),  new ThreadPoolExecutor.AbortPolicy());

    public String select(String name, String value) {
//        timeService.getOrderInfo();
//        timeService.getUserInfo();
//        timeService.getOtherInfo();
        Callable<String> getOrderInfo = new Callable<String>() {
            public String call() throws Exception {
                String orderInfo = timeService.getOrderInfo();
                return orderInfo;
            }
        };
        Callable<String> getUserInfo = new Callable<String>() {
            public String call() throws Exception {
                String userInfo = timeService.getUserInfo();
                return userInfo;
            }
        };
        Callable<String> getOtherInfo = new Callable<String>() {
            public String call() throws Exception {
                String otherInfo = timeService.getOtherInfo();
                return otherInfo;
            }
        };

        FutureTask<String> orderTask = new FutureTask<String>(getOrderInfo);
        FutureTask<String> userTask = new FutureTask<String>(getUserInfo);
        FutureTask<String> otherTask = new FutureTask<String>(getOtherInfo);
        executorService.submit(orderTask);
        executorService.submit(userTask);
        executorService.submit(otherTask);
        String orderTaskStr = null;
        String userInfoStr = null;
        String otherInfoStr = null;
        try {
            orderTaskStr = orderTask.get();
            userInfoStr = userTask.get();
            otherInfoStr = otherTask.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return "name===="+name+"; value==="+value+"...."+orderTaskStr+"..."+userInfoStr+"..."+otherInfoStr;
    }
}
